﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CSF.BusinessCore.BLL;
using CSF.Customers.Data;
using Ict.DataAccess;

namespace CSF.Customers.BLL
{
    public class CustomerUserApplyBLL : TransactionLogic<CustomerUserApply>
    {
        /// <summary>
        /// 获取指定审批状态且活动的客户集合
        /// </summary>
        /// <param name="status">审批状态</param>
        /// <returns>客户集合</returns>
        public List<CustomerUserApply> GetByApprovalStatus(ApprovalStatus status)
        {
            var q = from p in DataContext.Select<CustomerUserApply>()
                    where p.ApproveStatus == status
                    select p;
            return q.ToList();
        }

        /// <summary>
        /// 审批通过一个用户加入指定客户的申请
        /// </summary>
        /// <param name="id">申请Id</param>
        /// <param name="customerId">客户Id</param>
        /// <param name="approveComment">审批备注</param>
        /// <param name="approveBy">审批人</param>
        /// <returns>是否成功</returns>
        public bool Approve(int id, int customerId, string approveComment, string approveBy)
        {
            CustomerUserApply obj = this.GetOneById(id);
            obj.ApproveStatus = ApprovalStatus.Approved;
            obj.ApproveBy = approveBy;
            obj.ApproveComment = approveComment;
            obj.ApproveTime = DateTime.Now;

            CustomerUser customerUser = new CustomerUser()
            {
                UserId = obj.UserId,
                CustomerId = customerId,
                CreateBy = approveBy
            };
            CustomerUserBLL bll = new CustomerUserBLL();
            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();

                    bll.Delete(p => p.UserId == obj.UserId && p.CustomerId == obj.CustomerId, exec);
                    this.Update(obj, p => new { p.ApproveStatus, p.ApproveComment, p.ApproveBy, p.ApproveTime }, p => p.Id == id, exec);
                    bll.Insert<CustomerUser>(customerUser, null, exec);

                    exec.CommitTransaction();
                }
                catch (PrimaryKeyException)
                {
                    exec.CommitTransaction();
                }
                catch (Exception)
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }
            return true;
        }

        /// <summary>
        /// 审批不通过一个用户加入指定客户的申请
        /// </summary>
        /// <param name="id">申请Id</param>
        /// <param name="approveComment">审批备注</param>
        /// <param name="approveBy">审批人</param>
        /// <returns>是否成功</returns>
        public bool Disapprove(int id, string approveComment, string approveBy)
        {
            CustomerUserApply obj = this.GetOneById(id);
            obj.ApproveStatus = ApprovalStatus.Disapproved;
            obj.ApproveBy = approveBy;
            obj.ApproveComment = approveComment;
            obj.ApproveTime = DateTime.Now;

            CustomerUserBLL bll = new CustomerUserBLL();
            using (Executor exec = DataContext.CreateExecutor())
            {
                try
                {
                    exec.BeginTransaction();

                    bll.Delete(p => p.UserId == obj.UserId && p.CustomerId == obj.CustomerId, exec);
                    this.Update(obj, p => new { p.ApproveStatus, p.ApproveComment, p.ApproveBy, p.ApproveTime }, p => p.Id == id, exec);

                    exec.CommitTransaction();
                }
                catch (Exception)
                {
                    exec.RollbackTransaction();
                    throw;
                }
            }
            return true;
        }
    }
}
